import java.util.ArrayList;
import java.util.Stack;

public class StackImpl {
	private ArrayList<String> stack = null;
	private int CAPACITY = 3;
	private int STACK_POINTER = 0;

	public StackImpl() {
		stack = new ArrayList<String>();
	}

	public void push(String item) throws StackException {
		if (STACK_POINTER >= 3) {
			throw new StackException(StackException.STACK_OVERFLOW);
		}

		stack.add(STACK_POINTER, item);
		STACK_POINTER++;
		System.out.println("Index: " + STACK_POINTER + "  Added: "
				+ item.toString());
	}

	public Object pop() throws StackException {
		Object value = -1;
		if (STACK_POINTER == 0) {
			throw new StackException(StackException.STACK_UNDERFLOW);
		}
		STACK_POINTER--;
		value = stack.get(STACK_POINTER);
		System.out.println("Value POPPED: " + value.toString());
		return value;
	}

	public String toString() {
		StringBuffer stringBuffer = new StringBuffer("");
		if (STACK_POINTER == 0) {
			return null;
		}		
			for (int index = STACK_POINTER - 1; index >= 0; index--) {
				stringBuffer.append("\n");
				stringBuffer.append(stack.get(index));				
			}		
		return stringBuffer.toString();
	}
}
